package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.comm.BaseController;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.PushMessage;
import cn.nawang.ebeim.api.domain.User;
import cn.nawang.ebeim.api.service.PushMessageService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.DateOperate;
import cn.nawang.ebeim.api.utils.DateUtils;

/**
 * @ClassName: PushMessageController
 * @Description: TODO
 * @author fanyb
 * @date Oct 14, 2015 2:48:02 PM
 * @version v1.0
 */
@RestController
public class PushMessageController extends BaseController {
  private static final Logger LOG = LoggerFactory.getLogger(PushMessageController.class);

  @Autowired
  private PushMessageService pushMessageService;

  /**
   * 
   * @author fanyb
   * @Description: 新增对象
   * @param pushMessage
   * @param request
   * @return Map<String,Object>
   * @date Oct 14, 20153:29:35 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/push_message/add", method = RequestMethod.POST)
  public Map<String, Object> add(PushMessage pushMessage, HttpServletRequest request) {
    LOG.debug("/push_message/add {}", pushMessage);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    User user = this.getUserLoginInfo(request);
    boolean addFlag = pushMessageService.add(pushMessage, user);
    if (addFlag) {
      map.put("code", Constant.CODE_SUCCESS);
    }
    LOG.debug("add {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查询列表
   * @param page
   * @param request
   * @return Object
   * @date Oct 14, 20153:33:48 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/push_message/list", method = RequestMethod.GET)
  public Object findAllByPaging(Pages page, HttpServletRequest request) {
    LOG.debug("/push_message/list {}", page);
    Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // 推送消息
    String pushMessageTitle = request.getParameter("pushMessageTitle");
    if (!StringUtils.isEmpty(pushMessageTitle)) {
      dtovect.add(new SearchFormParams("bl.pushMessageTitle", pushMessageTitle, "pushMessageTitle",
          SearchFormParams.STRING_TYPE));
    }
    // 起始时间
    String startTime = request.getParameter("startTime");
    if (!StringUtils.isEmpty(startTime)) {
      Date date = DateOperate.formatStringToDate(startTime, DateOperate.DEFAULTSTYLE);
      dtovect.add(new SearchFormParams("bl.createTime", date.getTime(), "startTime",
          SearchFormParams.START_TIME_TYPE));
    }

    // 结束时间
    String endTime = request.getParameter("endTime");
    if (!StringUtils.isEmpty(endTime)) {
      Date date = DateOperate.formatStringToDate(endTime, DateOperate.DEFAULTSTYLE);
      long endTimeLong = DateUtils.getDateEndTime(date);
      dtovect.add(new SearchFormParams("bl.createTime", endTimeLong, "endTime",
          SearchFormParams.END_TIME_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = pushMessageService.findPageList(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findAllByPaging {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查询id之前的列表
   * @param id
   * @return Object
   * @date Oct 14, 20154:51:33 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/push_message/list/id", method = RequestMethod.GET)
  public Object findMoreThan(Integer id) {
    LOG.debug("/token/push_message/list/id {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pushMessageService.findMoreThan(id));
    LOG.debug("findMoreThan {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 批量删除
   * @param ids
   * @return Object
   * @date Oct 14, 20155:28:36 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/push_message/delete", method = RequestMethod.GET)
  public Object delete(int[] ids) {
    LOG.debug("/push_message/delete {}", ids);
    Map<String, Object> map = new HashMap<String, Object>();
    pushMessageService.delete(ids);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    LOG.debug("delete {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找一个对象
   * @param id
   * @return Object
   * @date Oct 14, 20155:51:31 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/push_message/id", method = RequestMethod.GET)
  public Object findOne(int id) {
    LOG.debug("/push_message/id {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    PushMessage pushMessage = pushMessageService.findOne(id);
    if (pushMessage == null) {
      map.put(Constant.JSON_CODE, Constant.CODE_404);
    } else {
      map.put(Constant.JSON_CODE, Constant.CODE_200);
      map.put("obj", pushMessage);
    }
    LOG.debug("findOne {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新对象
   * @param pushMessage
   * @return Object
   * @date Oct 14, 20156:17:38 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/push_message/update", method = RequestMethod.POST)
  public Object update(PushMessage pushMessage) {
    LOG.debug("/push_message/update {}", pushMessage);
    Map<String, Object> map = new HashMap<String, Object>();
    pushMessageService.update(pushMessage);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    LOG.debug("update {}", map);
    return map;
  }
}
